package com.prb.service.impl;

import com.prb.annotation.Autowrite;
import com.prb.annotation.Compoant;
import com.prb.annotation.Transactional;
import com.prb.dao.AccountDao;
import com.prb.entity.Account;
import com.prb.service.AccountService;

import java.sql.SQLException;

@Compoant
public class AccountServiceImpl implements AccountService {

    @Autowrite
    AccountDao accountDao;

    @Override
    public Account queryAccount(String cardNo) throws SQLException {

        return accountDao.queryAccount(cardNo);
    }

    @Override
    @Transactional
    public boolean doTrans(String from , String to , Integer money) throws Exception {

       /* try {
            //开启事务
            TransactionManager.beginTx();*/
            Account fromAccount = queryAccount(from);
            Integer reeMoney = fromAccount.getMoney() - money;
            accountDao.doTrans(from , reeMoney);

            //int i = 10 / 0;
            Account toAccount = queryAccount(to);
            Integer addMoney = toAccount.getMoney() + money;
            accountDao.doTrans(to , addMoney);
            //提交事务
     /*       TransactionManager.commit();
        }catch (Exception e){
            //回滚事务
            TransactionManager.rollback();
            throw e;
        }*/
        return true;
    }
}
